<%@page import="com.infolab.aims.domain.workorder.WorkOrderAdHoc"%>
<%@ include file="/WEB-INF/views/taglib.jsp" %>
<h3>Work Order Form</h3>	
	<form:form id="form" method="post" modelAttribute="workOrder" cssClass="simpleForm workOrderForm">
		<form:hidden path="accessDepartment.departmentId" />
		<form:hidden path="workOrderId" />
		<form:hidden path="workOrderCode" />
		<form:hidden path="vehicleId" />
		<form:hidden path="workOrderType" />
		<form:hidden path="driverNric" />
		<form:hidden path="driverName" />
		<form:hidden path="driverTelephoneNo" />
		<form:hidden path="driverEmail" />
		<form:hidden path="driverType" />
		<form:hidden path="meVersion" />
		 <div class="action">
	  	<c:choose>
	  		<c:when test="${workOrder.workOrderId!=null}">
  				<button type="submit"  class="button positive updateBtn">Update</button>
  				<button type="button" class="button positive closeBtn" onclick="closeWorkOrder()">Close Work Order</button>
  				<a href="<c:url value="/workOrder/print/${workOrder.workOrderId}" />" target="_blank" class="button">Print</a>
  				<button type="button" class="button positive cancelBtn" onclick="cancelWorkOrder()">Cancel Work Order</button>
  				<a href="<c:url value="/workOrder/search" />" class="button">Back</a>
	  		</c:when>
	  	</c:choose>
	 	</div>	
	 	<div class="clear" style="height:1px">&nbsp;</div>
		<jsp:include page="/WEB-INF/views/message.jsp"></jsp:include>
		<div class="tabs clearfix">
        <ul>
        	<li class="active"><span><a href="<c:url value="/workOrder/update/${workOrder.workOrderId}" />">Taxi Details</a></span></li>
            <li class="active">
            <img src="${imageUrl}/tab_active.jpg">
            <span>Complaint/Repair</span>
            </li>
        </ul>
        </div>
        <div class="content">
        	<div class="CD_contentStatus clearfix">
            	<span class="left">Work Order Code : <strong>
				${workOrder.workOrderCode}</strong></span>
                <span class="right">Status : <strong>
                <c:choose>
                	<c:when test="${workOrder.workOrderStatus=='CLOSED'}">
                		<span class="red padding4">		
                	</c:when>
                	<c:when test="${workOrder.workOrderStatus=='CANCEL'}">
                		<span class="grey padding4">		
                	</c:when>
                	<c:when test="${workOrder.workOrderStatus=='INQUEUE'}">
                		<span class="blue padding4">		
                	</c:when>
                	<c:when test="${workOrder.workOrderStatus=='INPROCESS'}">
                		<span class="yellow padding4">		
                	</c:when>
                	<c:when test="${workOrder.workOrderStatus=='COMPLETED'}">
                		<span class="green padding4">		
                	</c:when>
                	<c:otherwise>
                		<span class="blue padding4">INQUEUE
                	</c:otherwise>
                </c:choose>
                
                ${workOrder.workOrderStatus}
                </span></strong></span>
        	</div>
        	<fieldset>
	  		<div class="head">
	  		COMPLAINT LIST
	  		<span class="right">
	  		<a href="<c:url value="/workOrder/update/${workOrder.workOrderId}?tab=2" />" class="CD_addNew addRow" title="Refresh">
				 <img align="absmiddle" src="${imageUrl}/icon_update.png"> Refresh
			</a>
	  		<a class="CD_addNew addRow" href="javascript:void(0)" onclick="addComplaintRow()" title="Add Row">
	  		<img align="absmiddle" src="${imageUrl}/icon_add.png"> Add Row
	  		</a>
	  		
	  		</span>
	  		</div>
	  		<table class="gridSearch"  id="complaintTable">
	  		 <col width="25%">
 			 <col width="30%">
 			 <col width="35%">
 			 <col width="10%">
			 <thead>	
			 	<tr>
			 		<th>Model</th>
			 		<th>Complaint 
			 		&nbsp;&nbsp;<a href="javascript:void(0)" onclick="popUpComplaints()" title="Show Complaints list"><img alt="Show Complaints list" src="${imageUrl }/show.png"></a>
			 		</th>
			 		<th>Remarks</th>
			 		<th>Action</th>
			 	</tr>
			 </thead>
			 
			 <tbody>
			 	<c:forEach items="${workOrder.workOrderComplaints}" var="item" varStatus="status">
			 		<tr <c:if test="${empty item.workOrderDetailId}">class="new"</c:if>>
			 		<td>
			 			<input type="hidden" name="workOrderComplaints[${status.index}].workOrderDetailId" value="${item.workOrderDetailId}">
			 			<input type="hidden" name="workOrderComplaints[${status.index}].workOrder.workOderId" value="${workOrder.workOrderId}">
				 		<input type="hidden" name="workOrderComplaints[${status.index}].deleted" value="false" />
						<select name="workOrderComplaints[${status.index}].assetModel.assetModelId">
							<c:forEach items="${assetModelList}" var="item2">								
								<option value="${item2.assetModelId}" <c:if test="${item.assetModel.assetModelId==item2.assetModelId}">selected</c:if>>${item2.assetModelName}</option>
							</c:forEach>
						</select>
			 		</td>
			 		<td>
			 			<input type="hidden" name="workOrderComplaints[${status.index}].genericCode.systemCodeValueId" value="${item.genericCode.systemCodeValueId}" class="faultCodeId" >
			 			<input type="hidden" name="workOrderComplaints[${status.index}].genericCode.systemCode1" value="${item.genericCode.systemCode1}" class="faultCode" >
			 			<textarea class="faultDescription" rows="2" cols="30" name="workOrderComplaints[${status.index}].genericCode.systemCodeValueDescription">${item.genericCode.systemCodeValueDescription}</textarea>
			 			 
			 		</td>
			 		<td>
			 			<textarea name="workOrderComplaints[${status.index}].workOrderDetailRemarks" rows="2" cols="30">${item.workOrderDetailRemarks}</textarea> 
			 		</td>
			 		<td>
			 			<a href="javascript:void(0)" onclick="removeComplaintRow(this)" title="Delete Row">
	  						<img align="absmiddle" src="${imageUrl}/icon_delete.png">
	  					</a>
			 			 
			 		</td>
			 		</tr>
			 	</c:forEach>
			 </tbody>
			 </table>
	  		</fieldset>
	  		<fieldset>
	  		<div class="head">
	  		WORK DONE
	  		<span>
	  		<a href="<c:url value="/workOrder/update/${workOrder.workOrderId}?tab=2" />" class="CD_addNew addRow" title="Refresh">
				 <img align="absmiddle" src="${imageUrl}/icon_update.png"> Refresh
			</a>
	  		<a class="CD_addNew" href="javascript:void(0)" onclick="addRepairRow()" title="Add Row">
	  		<img align="absmiddle" src="${imageUrl}/icon_add.png"> Add Row
	  		</a>
	  		</span>
	  		</div>
	  		<table class="gridSearch"  id="repairTable">
	  		 <col width="25%">
 			 <col width="30%">
 			 <col width="35%">
 			 <col width="10%">
			 <thead>	
			 	<tr>
			 		<th>Model</th>
			 		<th>Repair 
			 		&nbsp;&nbsp;<a href="javascript:void(0)" onclick="popUpRepairs()" title="Show Repairs list"><img alt="Show Repairs list" src="${imageUrl }/show.png"></a></th>
			 		<th>Remarks</th>
			 		<th>Action</th>
			 	</tr>
			 </thead>
			 
			 <tbody>
			 	<c:forEach items="${workOrder.workOrderRepairs}" var="item" varStatus="status">
			 		<tr>
			 		<td>
			 			<input type="hidden" name="workOrderRepairs[${status.index}].workOrderDetailId" value="${item.workOrderDetailId}">
			 			<input type="hidden" name="workOrderRepairs[${status.index}].workOrder.workOderId" value="${workOrder.workOrderId}">
				 		<input type="hidden" name="workOrderRepairs[${status.index}].deleted" value="false" />					
						<select name="workOrderRepairs[${status.index}].assetModel.assetModelId">
						<c:forEach items="${assetModelList}" var="item2">								
							<option value="${item2.assetModelId}" <c:if test="${item.assetModel.assetModelId==item2.assetModelId}">selected</c:if>>${item2.assetModelName}</option>
						</c:forEach>

			 		</td>
			 		<td>
			 			<input type="hidden" name="workOrderRepairs[${status.index}].genericCode.systemCodeValueId" value="${item.genericCode.systemCodeValueId}" class="repairCodeId" >
			 			<input type="hidden" name="workOrderRepairs[${status.index}].genericCode.systemCode1" value="${item.genericCode.systemCode1}" class="repairCode" >
			 			<textarea class="repairDescription" rows="2" cols="30" name="workOrderRepairs[${status.index}].genericCode.systemCodeValueDescription">${item.genericCode.systemCodeValueDescription}</textarea> 
			 		</td>
			 		<td>
			 			<textarea name="workOrderRepairs[${status.index}].workOrderDetailRemarks" cols="30" rows="2" >${item.workOrderDetailRemarks}</textarea> 
			 		</td>
			 		<td>
			 			<a href="javascript:void(0)" onclick="removeRepairRow(this)" title="Delete Row">
	  						<img align="absmiddle" src="${imageUrl}/icon_delete.png">
	  					</a> 
			 		</td>
			 		</tr>
			 	</c:forEach>
			 </tbody>
			 </table>
	  		</fieldset>
	  		<fieldset>
	  		<div class="head">
	  		IN-VEHICLE
	  		<span>
	  		<a class="CD_addNew" href="javascript:void(0)" onclick="addInVehicleRow()" title="Add Row">
	  		<img align="absmiddle" src="${imageUrl}/icon_add.png"> Add Row
	  		</a>
	  		</span>
	  		</div>
	  		<table class="gridSearch"  id="inVehicleTable">
	  		 <col width="25%">
 			 <col width="15%">
 			 <col width="15%">
 			 <col width="35%">
 			 <col width="10%">
			 <thead>	
			 	<tr>
			 		<th>Model</th>
			 		<th>Old Serial No</th>
			 		<th>New Serial No</th>
			 		<th>Remarks</th>
			 		<th>Action</th>
			 	</tr>
			 </thead>
			 <tbody>
			 	<c:forEach items="${workOrder.workOrderParts}" var="item" varStatus="status">
			 		<tr valign="top">
			 		<td valign="top">
			 			<input type="hidden" name="workOrderParts[${status.index}].workOrderPartId" value="${item.workOrderPartId}">
			 			<input type="hidden" name="workOrderParts[${status.index}].workOrder.workOderId" value="${workOrder.workOrderId}">
				 		<input type="hidden" name="workOrderParts[${status.index}].deleted" value="false" />
						<select name="workOrderParts[${status.index}].assetModel.assetModelId"  onchange="changeParamSerialNo(this)" class="assetModelId">
						<c:forEach items="${assetModelList}" var="item2">								
							<option value="${item2.assetModelId}" <c:if test="${item.assetModel.assetModelId==item2.assetModelId}">selected</c:if>>${item2.assetModelName}</option>
						</c:forEach>
					</select>
			 		</td>
			 		<td>
			 			<input type="text" name="workOrderParts[${status.index}].oldSerialNo" value="${item.oldSerialNo}" class="oldSerialNo">			 			
			 		</td>
			 		<td>
			 			<input type="text" name="workOrderParts[${status.index}].newSerialNo" value="${item.newSerialNo}" class="newSerialNo" 	> 
			 		</td>
			 		<td>
			 			<textarea name="workOrderParts[${status.index}].workOrderPartRemarks" cols="30" rows="2">${item.workOrderPartRemarks}</textarea> 
			 		</td>
			 		<td>
			 		<c:if test="${empty item.oldSerialNo}">
			 			<a href="javascript:void(0)" onclick="removeInVehicleRow(this)" title="Delete Row">
	  						<img align="absmiddle" src="${imageUrl}/icon_delete.png">
	  					</a> 
	  				</c:if>
			 		</td>
			 		</tr>
			 	</c:forEach>
			 </tbody>
			 </table>
	  		</fieldset>
	  		
	  		<fieldset>
	  		<legend>Technician</legend>
	  		<div>
	  		<p class="last clearfix">
	  			<label>NRIC</label>
	  			<input type="hidden" name="technicianId" id="technicianId" /> 
	  			<input class="left" type="text" name="technicianNric" id="technicianNric" />
	  			<a id="reassignBtn" class="button zeromargin" onclick="reAssignTechnician(this)" href="javascript:void(0)">Re-Assign</a>
	  		</p>
	  		<p>
	  			<label>Name</label>
	  			<span id="technicianName"></span>
	  		</p>
	  		<hr />
	  		</div>
	  		<table class="gridSearch"  id="tehnicianTable">
			 <thead>	
			 	<tr>
			 		<th>Technician</th>
			 		<th>Start Time</th>
			 		<th>End Time</th>			 		
			 	</tr>
			 </thead>
			 <tbody>
			 	<c:forEach items="${workOrder.workOrderTehnicians}" var="item" varStatus="status">
			 		<tr>
			 		<td>
			 		<sec:authorize access="authenticated and hasRole('ACCESS_SUPER_ADMIN')">
 					<a href="#" onclick="editTime('${item.workOrderTechnicianId}')">
 					</sec:authorize>
			 			${item.technician.personName}
			 		<sec:authorize access="authenticated and hasRole('ACCESS_SUPER_ADMIN')">
			 		</a>
			 		</sec:authorize>
			 		
			 		</td>
			 		<td>
			 			<fmt:formatDate value="${item.timeStart}" pattern="${globalDateTimeFormatJava}"/>			 		 
			 		</td>
			 		<td>
			 			<fmt:formatDate value="${item.timeEnd}" pattern="${globalDateTimeFormatJava}"/>	 
			 		</td>			 		
			 					 		
			 		</tr>
			 	</c:forEach>
			 </tbody>
			 </table>
	  		</fieldset>
	  		</div>
	  	
      <div class="action">
	  	<c:choose>
	  		<c:when test="${workOrder.workOrderId!=null}">
  				<button type="submit"  class="button positive updateBtn">Update</button>
  				<button type="button" class="button positive closeBtn" onclick="closeWorkOrder()">Close Work Order</button>
  				<a href="<c:url value="/workOrder/print/${workOrder.workOrderId}" />" target="_blank" class="button">Print</a>
  				<button type="button" class="button positive cancelBtn" onclick="cancelWorkOrder()">Cancel Work Order</button>
  				<a href="<c:url value="/workOrder/search" />" class="button">Back</a>
	  		</c:when>
	  	</c:choose>
	 </div>	
	</form:form>
	
	<table id="tmpRepairTable" style="display:none">
		<tr class="new">
		<td>
			<input name="workOrderRepairs.workOrder.workOrderId" type="hidden" />
			<input name="workOrderRepairs.workOrderDetailId" type="hidden" />
			<select name="workOrderRepairs.assetModel.assetModelId">
				<c:forEach items="${assetModelList}" var="item2">								
					<option value="${item2.assetModelId}">${item2.assetModelName}</option>
				</c:forEach>
			</select>
	  	</td>
	  	<td>
	  	<input name="workOrderRepairs.genericCode.systemCodeValueId" type="hidden" class="repairCodeId" />
	  	<input name="workOrderRepairs.genericCode.systemCode1" type="hidden" class="repairCode" />
	  	<textarea class="repairDescription" rows="2" cols="30" name="workOrderRepairs.genericCode.systemCodeValueDescription"></textarea>
	  	</td>
	  	<td>
	  	<textarea name="workOrderRepairs.workOrderDetailRemarks" cols="30" rows="2"></textarea>
	  	</td>
	  	<td>
	  		<a href="javascript:void(0)" onclick="removeRepairRow(this)" title="Delete Row">
	  			<img align="absmiddle" src="${imageUrl}/icon_delete.png">
	  		</a>
	  	</td>
		</tr>
	</table>
	
	<table id="tmpInVehicleTable" style="display:none">
		<tr class="new">
		<td>
			<input name="workOrderParts.workOrder.workOrderId" type="hidden" />
			<input name="workOrderParts.workOrderPartId" type="hidden" />
			<select name="workOrderParts.assetModel.assetModelId" class="assetModelId"  onchange="changeParamSerialNo(this)">
				<c:forEach items="${assetModelList}" var="item2">								
					<option value="${item2.assetModelId}">${item2.assetModelName}</option>
				</c:forEach>
			</select>
	  	</td>
	  	<td>
	  		<input name="workOrderParts.oldSerialNo" type="text" class="oldSerialNo" />
	  	</td>
	  	<td>
	  		<input name="workOrderParts.newSerialNo" type="text" class="newSerialNo" />
	  	</td>
	  	<td>
	  		<textarea name="workOrderParts.workOrderPartRemarks" rows="2" cols="30"></textarea>
	  	</td>
	  	<td>
	  		<a href="javascript:void(0)" onclick="removeInVehicleRow(this)" title="Delete Row">
	  			<img align="absmiddle" src="${imageUrl}/icon_delete.png">
	  		</a> 
	  	</td>
		</tr>
	</table>
	
	<table id="tmpComplaintTable" style="visibility:hidden">
		<tr class="new">
		<td>
			<input name="workOrderComplaints.workOrder.workOrderId" type="hidden" />
			<input name="workOrderComplaints.workOrderDetailId" type="hidden" />
			
			<input name="workOrderComplaints.assetModel.assetModelId" type="hidden" class="assetModelId" />
			<select name="workOrderComplaints.assetModel.assetModelId">
				<c:forEach items="${assetModelList}" var="item">
					<option value="${item.assetModelId}">${item.assetModelName}</option>
				</c:forEach>
			</select>
	  	</td>
	  	<td>
	  	<input name="workOrderComplaints.genericCode.systemCodeValueId" type="hidden" class="faultCodeId" />
	  	<input name="workOrderComplaints.genericCode.systemCode1" type="hidden" class="faultCode" />
	  	<textarea name="workOrderComplaints.genericCode.systemCodeValueDescription" class="faultDescription" rows="2" cols="30"></textarea>
	  	</td>
	  	<td>
	  	<textarea name="workOrderComplaints.workOrderDetailRemarks" rows="2" cols="30"></textarea>
	  	</td>
  		<td>
			<a href="javascript:void(0)" onclick="removeComplaintRow(this)" title="Delete Row">
				<img align="absmiddle" src="${imageUrl}/icon_delete.png">
			</a>
		</td>
		</tr>
	</table>
	<script type="text/javascript">
	
		var repairCode;
		var newWin;
		var complaintCodeComplete = false;
		var repairCodeComplete = false;
		$(document).ready(function() {
			
			$("#waitCommonDialog").dialog('open');
			
			<c:if test="${workOrder.workOrderStatus=='CLOSED' || workOrder.workOrderStatus=='CANCEL' }">
				$('.updateBtn').hide();
				$('.closeBtn').hide();
				$('.cancelBtn').hide();
			</c:if>
			
			$('.assetModelId').change();
			
			$.getJSON("<c:url value="/ajax/repairCode"></c:url>", function(data) {
				repairCode=data;
				autoCompleteRepairCode();
			});
			

			$.getJSON("<c:url value="/ajax/faultCode"></c:url>", function(data) {
				faultCode=data;
				autoCompleteFaultCode();
			});
			
			
			$("#technicianNric").autocomplete("<c:url value="/ajax/technician"></c:url>", {
		        dataType: 'json',
		        highlight: false,
		        scroll: true,
		        scrollHeight: 300,
		        autoFill: true,
				mustMatch: true,
				selectFirst:true,
		        parse: function(data) {
	                var array = new Array();
	                for(var i=0;i<data.length;i++)
	                {
	                    array[i] = { data: data[i], value: data[i].nric, result: data[i].nric};
	                }
	                return array;
		        },

		        formatItem: function(row) {
		             return row.nric;
		        }
		    });
			
			$("#technicianNric").result(autoCompleteTechnician);
			
			autoCompleteSerialNoEquipment();
			
		});
		
		function autoCompleteTechnician(event, data, formatted)
		{
			if (data)
			{
				$('#technicianName').text(data.personName);
				$('#technicianId').val(data.userName);
			}
		}
		
		function autoCompleteAssetModelEquipmentHandler(event, data, formatted)
		{
			if (data)
			{
				$(this).parent().find(".assetModelId").val(data.assetModelId);
				autoCompleteSerialNoEquipment();
			}
		}
		
		function editTime(techId){	
			
				var url = "<c:url value="/workOrder/editWorkOrderTime/${workOrder.workOrderId}"></c:url>?techId="+techId;
				newWin = window.open(url,'_blank',"height=500,width=1100,resizable=yes,scrollbars=yes");  
				newWin.focus() ;
		}
		
		
		function addRepairRow()
		{
			
			var row = $('#tmpRepairTable tr').clone();
			var rowSize = $('#repairTable tbody tr').size();			
			$('input[name=workOrderRepairs.workOrder.workOrderId]',row).attr('name','workOrderRepairs['+rowSize+'].workOrder.workOrderId');
			$('input[name=workOrderRepairs.workOrderDetailId]',row).attr('name','workOrderRepairs['+rowSize+'].workOrderDetailId');
			
			$('select[name=workOrderRepairs.assetModel.assetModelId]',row).attr('name','workOrderRepairs['+rowSize+'].assetModel.assetModelId');
			
			$('input[name=workOrderRepairs.genericCode.systemCodeValueId]',row).attr('name','workOrderRepairs['+rowSize+'].genericCode.systemCodeValueId');
			$('input[name=workOrderRepairs.genericCode.systemCode1]',row).attr('name','workOrderRepairs['+rowSize+'].genericCode.systemCode1');
			$('textarea[name=workOrderRepairs.genericCode.systemCodeValueDescription]',row).attr('name','workOrderRepairs['+rowSize+'].genericCode.systemCodeValueDescription');
			
			$('textarea[name=workOrderRepairs.workOrderDetailRemarks]',row).attr('name','workOrderRepairs['+rowSize+'].workOrderDetailRemarks');
			//$('select[name=vendorAssets.assetModel.assetType]',row).attr('name','vendorAssets['+rowSize+'].assetModel.assetType');			
			
			row.appendTo('#repairTable tbody');
			
			autoCompleteRepairCode();
		}
		
		function removeRepairRow(obj)
		{
			var ctx = $(obj).parent().parent();
			if(ctx.hasClass('new'))
				ctx.remove();
			else
			{
				$('input[name*=deleted]',ctx).val('true');
				ctx.hide();
			}
		}
		
		
		function addInVehicleRow()
		{
			
			var row = $('#tmpInVehicleTable tr').clone();
			var rowSize = $('#inVehicleTable tbody tr').size();			
			$('input[name=workOrderParts.workOrder.workOrderId]',row).attr('name','workOrderParts['+rowSize+'].workOrder.workOrderId');
			$('select[name=workOrderParts.assetModel.assetModelId]',row).attr('name','workOrderParts['+rowSize+'].assetModel.assetModelId');
			$('input[name=workOrderParts.workOrderPartId]',row).attr('name','workOrderParts['+rowSize+'].workOrderPartId');
			
			$('input[name=workOrderParts.oldSerialNo]',row).attr('name','workOrderParts['+rowSize+'].oldSerialNo');
			$('input[name=workOrderParts.newSerialNo]',row).attr('name','workOrderParts['+rowSize+'].newSerialNo');
			$('textarea[name=workOrderParts.workOrderPartRemarks]',row).attr('name','workOrderParts['+rowSize+'].workOrderPartRemarks');
			//$('select[name=vendorAssets.assetModel.assetType]',row).attr('name','vendorAssets['+rowSize+'].assetModel.assetType');			
			
			row.appendTo('#inVehicleTable tbody');
			
			autoCompleteSerialNoEquipment();
			
		}
		
		function removeInVehicleRow(obj)
		{
			var ctx = $(obj).parent().parent();
			if(ctx.hasClass('new'))
				ctx.remove();
			else
			{
				$('input[name*=deleted]',ctx).val('true');
				ctx.hide();
			}
		}
		

		function reAssignTechnician(obj)
		{
			$(obj).attr('disabled','disabled');
			var technicianId = $('#technicianId').val();
			
			$.getJSON("<c:url value="/workOrder/reassign/${workOrder.workOrderId}"></c:url>",{userName : technicianId} ,function(data) {
				
				var timeStart = new Date(data.timeStart);
				<sec:authorize access="authenticated and hasRole('ACCESS_SUPER_ADMIN')">
				$('<tr><td><a href="#" onclick="editTime('+data.workOrderTechnicianId+')">'+data.technician.personName+'</a></td><td>'+formatDate(timeStart)+'</td><td></td></tr>').appendTo('#tehnicianTable tbody');
				</sec:authorize>
				<sec:authorize access="authenticated and !(hasRole('ACCESS_SUPER_ADMIN'))">
				$('<tr><td>'+data.technician.personName+'</td><td>'+formatDate(timeStart)+'</td><td></td></tr>').appendTo('#tehnicianTable tbody');
				</sec:authorize>
				$(obj).removeAttr('disabled');
			});
		}
		
		
		
		
		function autoCompleteRepairCode()
		{
			$(".repairDescription").autocomplete(repairCode, {
		        dataType: 'json',
		        highlight: false,
		        scroll: true,
		        scrollHeight: 300,
		        autoFill: true,
				mustMatch: true,
		        parse: function(data) {
	                var array = new Array();
	                for(var i=0;i<data.length;i++)
	                {
	                    array[i] = { data: data[i], value: data[i].systemCodeValueId, result: data[i].systemCodeValueId };
	                }
	                return array;
		        },

		        formatItem: function(row) {
		             return row.systemCodeValueDescription;
		        }
		    });
			
			$(".repairDescription").result(autoCompleteRepairCodeHandler);
			
			repairCodeComplete = true;
			
			if(repairCodeComplete && complaintCodeComplete)
				$("#waitCommonDialog").dialog('close');
		}
		
		function autoCompleteRepairCodeHandler(event, data, formatted)
		{
			if(data)
			{
				$(this).parent().find(".repairCodeId").val(data.systemCodeValueId);
				$(this).parent().find(".repairCode").val(data.systemCode1);
				//$(this).parent().find(".faultDescription").val(data.systemCodeValueDescription);
			}
		}
		
		function changeParamSerialNo(obj)
		{
			var ctx = $(obj).parent().parent();
			var autoComplete = $('.newSerialNo,.oldSerialNo',ctx);
			var id = $(obj).val();
			
			autoComplete.setOptions({
				extraParams : {assetModelId:id}
			});
		}
		function autoCompleteSerialNoEquipment()
		{
			
			$(".newSerialNo:visible,.oldSerialNo:visible").each(function(obj){
				var ctx = $(this).parent().parent();
				var assetModelId = ctx.find('.assetModelId').val();
				
				$(this).unautocomplete();
				
				if(assetModelId!=undefined)
				{
					//alert($(this).parent().parent().find('.assetModelId').val()+'x');
					$(this).autocomplete('<c:url value="/ajax/equipment/workOrder"></c:url>', {
					extraParams : {assetModelId: assetModelId},
					dataType: 'json',
					minChars:2,
			        highlight: false,
			        scroll: true,
			        scrollHeight: 300,
			        autoFill: true,
					mustMatch: true,
			        parse: function(data) {
		                var array = new Array();
		                for(var i=0;i<data.length;i++)
		                {
		                    array[i] = { data: data[i], value: data[i].assetEquipmentPK.serialNo, result: data[i].assetEquipmentPK.serialNo };
		                }
		                return array;
			        },
	
			        formatItem: function(row) {
			        	
			             return row.assetEquipmentPK.serialNo;
			        }
					});
				}
				
			});
			
			//alert(ctx.html());
			
		}
		
		function addComplaintRow(assetModel)
		{
			var row = $('#tmpComplaintTable tr').clone();
			var rowSize = $('#complaintTable tbody tr').size();			
			$('input[name=workOrderComplaints.workOrder.workOrderId]',row).attr('name','workOrderComplaints['+rowSize+'].workOrder.workOrderId');
			$('input[name=workOrderComplaints.workOrderDetailId]',row).attr('name','workOrderComplaints['+rowSize+'].workOrderDetailId');
			
			$('select[name=workOrderComplaints.assetModel.assetModelId]',row).attr('name','workOrderComplaints['+rowSize+'].assetModel.assetModelId');
			
			$('input[name=workOrderComplaints.genericCode.systemCodeValueId]',row).attr('name','workOrderComplaints['+rowSize+'].genericCode.systemCodeValueId');
			$('input[name=workOrderComplaints.genericCode.systemCode1]',row).attr('name','workOrderComplaints['+rowSize+'].genericCode.systemCode1');
			$('textarea[name=workOrderComplaints.genericCode.systemCodeValueDescription]',row).attr('name','workOrderComplaints['+rowSize+'].genericCode.systemCodeValueDescription');
			
			$('textarea[name=workOrderComplaints.workOrderDetailRemarks]',row).attr('name','workOrderComplaints['+rowSize+'].workOrderDetailRemarks');
			//$('select[name=vendorAssets.assetModel.assetType]',row).attr('name','vendorAssets['+rowSize+'].assetModel.assetType');			
			
			row.appendTo('#complaintTable tbody');
			
			autoCompleteFaultCode();
			
		}
		
		function removeComplaintRow(obj)
		{
			var ctx = $(obj).parent().parent();
			if(ctx.hasClass('new'))
				ctx.remove();
			else
			{
				$('input[name*=deleted]',ctx).val('true');
				ctx.hide();
			}
		}
		
		function autoCompleteFaultCode()
		{
			$(".faultDescription").autocomplete(faultCode, {
		        dataType: 'json',
		        highlight: false,
		        scroll: true,
		        scrollHeight: 300,
		        autoFill: true,
				mustMatch: true,
		        parse: function(data) {
	                var array = new Array();
	                for(var i=0;i<data.length;i++)
	                {
	                    array[i] = { data: data[i], value: data[i].systemCodeValueId, result: data[i].systemCodeValueId };
	                }
	                return array;
		        },

		        formatItem: function(row) {
		             return row.systemCodeValueDescription;
		        }
		    });
			
			$(".faultDescription").result(autoCompleteFaultCodeHandler);
			$(".faultDescription").attr('size','35');
			
			complaintCodeComplete = true;
			
			if(repairCodeComplete && complaintCodeComplete)
				$("#waitCommonDialog").dialog('close');
		}
		
		function autoCompleteFaultCodeHandler(event, data, formatted)
		{
			if(data)
			{
				$(this).parent().find(".faultCodeId").val(data.systemCodeValueId);
				$(this).parent().find(".faultCode").val(data.systemCode1);
				//$(this).parent().find(".faultDescription").val(data.systemCodeValueDescription);
			}
		}
		
		function popUpComplaints(){
						
			var url = "<c:url value="/systemCode/updateValue/5"></c:url>?popUpFlag=Y";
			openPopUp(url);			
		}
		
		function popUpRepairs(){
			
			var url = "<c:url value="/systemCode/updateValue/6"></c:url>?popUpFlag=Y";
			openPopUp(url);			
		}
		
		
		function openPopUp(url)  
		{  
		  var newwindow = window.open(url,'_blank',"height=500,width=1100,resizable=yes,scrollbars=yes");  
		  newwindow.focus() ;
		}
		
		function closeWorkOrder()
		{
			
			if(confirm('Do you really want to close the work order ? Please click OK to proceed')){
				$('#form').attr('action','<c:url value="/workOrder/close/${workOrder.workOrderId }"></c:url>');
				$('#form').submit();
			}
							
			
		}
		
		function cancelWorkOrder()
		{
			if(confirm('Do you really want to cancel the work order ? Please click OK to proceed')){
			$('#form').attr('action','<c:url value="/workOrder/cancel/${workOrder.workOrderId }"></c:url>');
			$('#form').submit();
			}
		}
		
		function reloadParent()
		 {			 
			 setTimeout('refreshList()', 50);			
		 }
		 
		 function refreshList() {
			 if (!newWin || newWin.closed) {
				 //window.location.href = window.location.href;
				 window.location.reload(true);
			 }
		 }
		 
	</script>
	